﻿namespace ufida.u9.ait.openapi
{
	using System;
    using System.Collections;
    using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base;
    using UFIDA.U9.PM.PO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// POCancelSV partial 
	/// </summary>	
	public partial class POCancelSV 
	{	
		internal BaseStrategy Select()
		{
			return new POCancelSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class POCancelSVImpementStrategy : BaseStrategy
	{
		public POCancelSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			POCancelSV bpObj = (POCancelSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");

				List<Hashtable> retlist = new List<Hashtable>();
				foreach (DocDTOData doc in bpObj.Data)
				{
					PurchaseOrder be = PurchaseOrder.Finder.Find($"DocNo='{doc.DocNo}'", null);
					if (be == null)
						throw new Exception($"单号：[{doc.DocNo}]查找不到数据");
					//if (be.Status != PODOCStatusEnum.Approving)
					//	throw new Exception($"单号：[{doc.DocNo}]状态不为提交，不能审核！");
					//if (be.POLines == null || be.POLines.Count == 0)
					//	throw new Exception($"单号：[{doc.DocNo}]行为空");
					//if (be.POLines[0].POShiplines == null || be.POLines[0].POShiplines.Count == 0)
					//	throw new Exception($"单号：[{doc.DocNo}]计划行为空");

					//先会退到开立态
					PORevocateSV sv=new PORevocateSV();
					sv.Data = new List<DocDTOData> { doc };
					sv.Do();

					//关闭处理过程
					using (ISession session = Session.Open())
					{
						be.Cancel.CancelUser = Context.LoginUser;
						be.Cancel.CancelReason = "外部终止";
						be.Cancel.Canceled = true;
						be.Cancel.CancelDate = DateTime.Now;

						session.Commit();
					}

					//处理返回
					Hashtable ret = new Hashtable();
					ret.Add("id", be.ID);
					ret.Add("docno", be.DocNo);
					retlist.Add(ret);
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}		
	}

	#endregion
	
	
}